package com.jaws.core.common.util;

import org.apache.commons.lang3.StringUtils;


/**
 * 
 * 脱敏工具类,敏感信息数据过滤
 * @author lixin
 * @date 2018-04-24 15:15:26
 * @since v1.0.0
 */
public class DesensitizeUtil {

	/**
	 * 真实姓名脱敏
	 * 
	 * @param realName
	 * @return
	 */
	public static String convertRealName(String realName) {
		if (StringUtils.isNotBlank(realName)) {
			int length = realName.length();
			if (length <= 1) {
				realName = "***";
			} else if (length == 2) {
				realName = "*".concat(realName.substring(length - 1, length));
			} else if (realName.length() == 3) {
				realName = "*".concat(realName.substring(length - 2, length));
			} else {
				realName = "**".concat(realName.substring(length - 2, length));
			}
		}
		return realName;
	}

	/**
	 * 登录用户名脱敏
	 * 
	 * @param accountName
	 * @return
	 */
	public static String convertAccountName(String accountName) {
		if (StringUtils.isNotBlank(accountName)) {
			// 如果账户是邮箱
			if (DataUtil.isEmail(accountName)) {
				return convertEmail(accountName);
			} else if (DataUtil.isMobile(accountName)) {
				return convertPhoneNum(accountName);
			} else {
				int length = accountName.length();
				if (length <= 1) {
					accountName = "****";
				} else if (length <= 2) {
					accountName = "****".concat(accountName.substring(length - 1, length));
				} else if (length > 2 && length <= 5) {
					accountName = accountName.substring(0, 1).concat("****").concat(accountName.substring(length - 1, length));
				} else {
					accountName = accountName.substring(0, 2).concat("****").concat(accountName.substring(length - 2, length));
				}
			}
		}
		return accountName;
	}

	/**
	 * 身份证脱敏 ,保留后前3位,后4位
	 * 
	 * @param idCard
	 * @return
	 */
	public static String convertIdCard(String idCard) {
		if (StringUtils.isNotBlank(idCard)) {
			int length = idCard.length();
			if (length >= 15) {
				idCard = idCard.substring(0, 3).concat("***********").concat(idCard.substring(length - 4, length));
			} else {
				idCard = "***********";
			}
		}
		return idCard;
	}

	/**
	 * 电子邮箱脱敏,@前保留前2位后2位
	 * 
	 * @param email
	 * @return
	 */
	public static String convertEmail(String email) {
		if (StringUtils.isNotBlank(email)) {
			String temp[] = email.split("@");
			String prefix = temp[0];
			int length = prefix.length();
			if (length <= 1) {
				prefix = "****";
			} else if (length == 2) {
				prefix = prefix.substring(0, 1).concat("****");
			} else if (length > 2 && length <= 5) {
				prefix = prefix.substring(0, 1).concat("****").concat(prefix.substring(length - 1, length));
			} else {
				prefix = prefix.substring(0, 2).concat("****").concat(prefix.substring(length - 2, length));
			}
			email = prefix + "@" + temp[1];
		}
		return email;
	}

	/**
	 * 手机号码脱敏, 保留前3位后4位
	 * 
	 * @param phone
	 * @return
	 */
	public static String convertPhoneNum(String phoneNun) {
		if (StringUtils.isNotBlank(phoneNun)) {
			int length = phoneNun.length();
			phoneNun = phoneNun.substring(0, 3).concat("****").concat(phoneNun.substring(length - 4, length));
		}
		return phoneNun;
	}

	/**
	 * 银行卡号码脱敏, 保留前4位,后3位
	 * 
	 * @param bankCard
	 * @return
	 */
	public static String convertBankCard(String bankCard) {
		if (StringUtils.isNotBlank(bankCard)) {
			int length = bankCard.length();
			bankCard = bankCard.substring(0, 4).concat("********").concat(bankCard.substring(length - 3, length));
		}
		return bankCard;
	}
	
	/**
	 * 微信号：前2, 后面***代替;
	 * 
	 * @param wechatNo
	 * @return
	 */
	public static String convertWeChatNo(String wechatNo) {
		if (StringUtils.isNotBlank(wechatNo)) {
			wechatNo = wechatNo.substring(0, 2).concat("********");
		}
		return wechatNo;
	}
	
	/**
	 * 微信昵称：前2后面***代替
	 * 
	 * @param wechatName
	 * @return
	 */
	public static String convertWeChatName(String wechatName) {
		if (StringUtils.isNotBlank(wechatName)) {
			wechatName = wechatName.substring(0, 2).concat("********");
		}
		return wechatName;
	}
	
	

	public static void main(String args[]) {
		System.out.println(convertRealName("欧阳雨"));
		System.out.println(convertAccountName("15626512035@qq.com"));
		System.out.println(convertEmail("cst@qq.com"));
		System.out.println(convertIdCard("452122198810170614"));
		System.out.println(convertPhoneNum("15626512035"));
		System.out.println(convertBankCard("6225887860680357"));
		System.out.println(convertWeChatNo("onebyte0755"));
	}
}
